package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.ShoppingCartBean;
import com.wwwarehouse.contract.bean.ShoppingCartListBean;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;

/**
 * Created by feng.gao on 2017/10/18.
 */

public class ShoppingCartAdapter extends BaseAdapter {
	private List<ShoppingCartListBean.ListBean> list = new ArrayList<>();
	private Context mContext;
	private int mHeight;
	private PlaceOrderListener placeOrderListener;

	public ShoppingCartAdapter(List<ShoppingCartListBean.ListBean> list, Context mContext, int mHeight) {
		this.list = list;
		this.mContext = mContext;
		this.mHeight = mHeight;
	}

	public void setPlaceOrderListener(PlaceOrderListener placeOrderListener) {
		this.placeOrderListener = placeOrderListener;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.contract_shopping_cart_item_layout, null);
			holder.mNameTv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.mMoneyTv = (TextView) convertView.findViewById(R.id.money);
			holder.mTipTv = (TextView) convertView.findViewById(R.id.tip_tv);
			holder.mConfirmBtn = (Button) convertView.findViewById(R.id.confirm_btn);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (list.size() > 0) {
			holder.mConfirmBtn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					placeOrderListener.toPlaceOrder(position);
				}
			});
			if (StringUtils.isNoneNullString(list.get(position).getSupplierName())) {
				holder.mNameTv.setText(list.get(position).getSupplierName());
			}
			if (StringUtils.isNoneNullString(list.get(position).getItemCount())) {
				if (Long.valueOf(list.get(position).getItemCount()) > 9999) {
					holder.mTipTv.setText(StringUtils.getResourceStr(mContext , R.string.goods_total_count , list.get(position).getItemCount() + "+"));
				} else {
					holder.mTipTv.setText(StringUtils.getResourceStr(mContext , R.string.goods_total_count , list.get(position).getItemCount()));
				}
			}
			if (StringUtils.isNoneNullString(list.get(position).getTotalPrice())) {
				holder.mMoneyTv.setText(StringUtils.getResourceStr(mContext , R.string.shopping_card_total_money , list.get(position).getTotalPrice()));
			}
			AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT, mHeight / 6);
			convertView.setLayoutParams(lp);

		}
		return convertView;
	}

	public class ViewHolder {
		private TextView mNameTv;
		private TextView mMoneyTv;
		private TextView mTipTv;
		private Button mConfirmBtn;
	}

	public interface PlaceOrderListener {
		void toPlaceOrder(int position);
	}


}
